package com.jx.lcs.common.baidu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.jx.lcs.util.common.GetProp;

/**
 * 百度推送工具类（iOS） 
 */
public class BaiduIOSPushUtil {
	
	private static Logger logger = LoggerFactory.getLogger(BaiduIOSPushUtil.class);
	
	private static final String B_BAIDU_API_KEY = GetProp.getConfig("B_BAIDU_API_KEY");
	
	private static final String B_BAIDU_SECRET_KEY = GetProp.getConfig("B_BAIDU_SECRET_KEY");
	
	private static final String B_BAIDU_API_KEY_APPSTORE = GetProp.getConfig("B_BAIDU_API_KEY_APPSTORE");
	
	private static final String B_BAIDU_SECRET_KEY_APPSTORE = GetProp.getConfig("B_BAIDU_SECRET_KEY_APPSTORE");
	
	//测试
	public static void main(String[] args) {
    	
		String user_id = "799366706699339185"; 
		
    	Long channel_id = 4909624981308888782L;

        String content = "{\"aps\":{\"alert\":\"double kill\", \"badge\":1}}";
        
        pushNotification(user_id, channel_id, content, null);
    }
	
	//推送通知
	public static void pushNotification(String user_id, Long channel_id, String content, String appType) {
		/*
         * @brief 推送单播通知(IOS APNS) message_type = 1 (默认为0)
         */
        // 1. 设置developer平台的ApiKey/SecretKey
		String baiduApiKey = B_BAIDU_API_KEY;
		String baiduSecretKey = B_BAIDU_SECRET_KEY;
		if(appType != null && "1".equals(appType)) {
			baiduApiKey = B_BAIDU_API_KEY_APPSTORE;
			baiduSecretKey = B_BAIDU_SECRET_KEY_APPSTORE;
		}
        ChannelKeyPair pair = new ChannelKeyPair(baiduApiKey, baiduSecretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
//                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            // 手机端的ChannelId， 手机端的UserId， 先用1111111111111代替，用户需替换为自己的
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(4); // device_type => 1: web 2: pc 3:android
                                      // 4:ios 5:wp
            request.setDeployStatus(2); // DeployStatus => 1: Developer 2:
                                        // Production
            request.setChannelId(channel_id);
            request.setUserId(user_id);

            request.setMessageType(1);
            request.setMessage(content);

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);

            // 6. 认证推送成功
//            logger.info("--消息推送完成，" + response.getSuccessAmount());

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
            
            logger.error("--消息推送客户端错误异常", e);
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            
            logger.error("--消息推送服务端错误异常，" + String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
	}
}
